package com.springboot.cloud.demos.producer.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.OrderForm;
import com.springboot.cloud.demos.entity.form.OrderQueryForm;
import com.springboot.cloud.demos.entity.form.OrderQueryForm;
import com.springboot.cloud.demos.entity.param.OrderQueryParam;
import com.springboot.cloud.demos.entity.po.Order;
import com.springboot.cloud.demos.producer.service.IOrderService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
@Api("order")
@Slf4j
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "orderForm", value = "新增产品form表单", required = true, dataType = "OrderForm")
    @PostMapping
    public Result<Order> add(@Valid @RequestBody OrderForm orderForm) {
        log.info("name:{}", orderForm);
        Order order = orderForm.toPo(Order.class);
        boolean res=orderService.add(order);
        if(res){
           return Result.success(order) ;
        }
        return Result.fail();
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(orderService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "orderForm", value = "产品实体", required = true, dataType = "OrderForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody OrderForm orderForm) {
        Order order = orderForm.toPo(Order.class);
        order.setId(id);
        return Result.success(orderService.update(order));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result<Order> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(orderService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "name", value = "产品名称", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result query() {
        OrderQueryParam orderQueryParam = new OrderQueryParam();
        return Result.success(orderService.query(orderQueryParam));
    }

    @ApiOperation(value = "搜索产品", notes = "根据条件查询产品信息")
    @ApiImplicitParam(name = "orderQueryForm", value = "产品查询参数", required = true, dataType = "OrderQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody OrderQueryForm orderQueryForm) {
        return Result.success(orderService.query(orderQueryForm.toParam(OrderQueryParam.class)));
    }
    @ApiOperation(value = "用户分页列表", notes = "根据条件查询产品信息，简单查询")
    @PostMapping("/page")
    public Result<Page>  page(@RequestBody OrderQueryForm orderQueryForm) {
        IPage res = orderService.page(orderQueryForm);
        return Result.success(res);
    }
}

